package com.zfsoft.zfoa.resource.dao;

import java.sql.SQLException;
import com.zfsoft.zfoa.dao.BasicDAO;
import com.zfsoft.zfoa.resource.entitys.Resource_info;

public class Resource_infoDAO extends BasicDAO {
	public Resource_info getInstance(String bh) {
		Resource_info resource_info = new Resource_info();
		try {
			String sql = "select * from resource_info where id=?";
			conn = this.getConn();
			ps = conn.prepareStatement(sql);
			ps.setString(1, bh);
			rs = ps.executeQuery();
			if (rs.next()) {
				resource_info.setId(rs.getString("id"));
				resource_info.setName(rs.getString("name"));
				resource_info.setManagerid(rs.getString("managerid"));
				resource_info.setManagername(rs.getString("managername"));
				resource_info.setNote(rs.getString("note"));
				resource_info.setType(rs.getString("type"));
				resource_info.setStatus(rs.getString("status"));
				resource_info.setMxdxid(rs.getString("mxdxid"));
				resource_info.setMxdxmc(rs.getString("mxdxmc"));
				resource_info.setMxdx(rs.getString("mxdx"));
				resource_info.setZindex(rs.getString("zindex"));
			}
			closeStmt();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			closeConn();
		}
		return resource_info;
	}

	public Boolean delete(String id) {
		Boolean res = true;
		String sql = "delete from resource_info where id =?";
		try {
			conn = this.getConn();
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);
			ps.setString(1, id);
			if (ps.executeUpdate() > 0) {
				res = true;
			} else {
				res = false;
			}
		} catch (SQLException e) {
			System.out.println(sql);
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				System.out.println(sql);
				e1.printStackTrace();
			}
			res = false;
		} finally {
			try {
				conn.commit();
			} catch (SQLException e) {
				System.out.println(sql);
				e.printStackTrace();
			}
			closeConn();
		}
		return res;
	}

	public Boolean update(String bh, Resource_info resource_info) {
		Boolean res = true;
		String sql = "";
		try {
			sql = "UPDATE resource_info SET id=?,name=?,managerid=?,managername=?,note=?,type=?,status=?,mxdxid=?,mxdxmc=?,mxdx=?,zindex=? WHERE id=?";
			conn = this.getConn();
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);
			ps.setString(1, resource_info.getId());
			ps.setString(2, resource_info.getName());
			ps.setString(3, resource_info.getManagerid());
			ps.setString(4, resource_info.getManagername());
			ps.setString(5, resource_info.getNote());
			ps.setString(6, resource_info.getType());
			ps.setString(7, resource_info.getStatus());
			ps.setString(8, resource_info.getMxdxid());
			ps.setString(9, resource_info.getMxdxmc());
			ps.setString(10, resource_info.getMxdx());
			ps.setString(11, resource_info.getZindex());
			ps.setString(12, bh);
			ps.executeUpdate();
			closeStmt();
		} catch (SQLException e) {
			System.out.println(sql);
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				System.out.println(sql);
				e1.printStackTrace();
			}
			res = false;
		} finally {
			try {
				conn.commit();
			} catch (SQLException e) {
				System.out.println(sql);
				e.printStackTrace();
			}
			closeConn();
		}
		return res;
	}

	public Boolean insert(Resource_info resource_info) {
		Boolean res = true;
		String sql = "";
		try {
			sql = "insert into resource_info(id,name,managerid,managername,note,type,status,mxdxid,mxdxmc,mxdx,zindex) values(?,?,?,?,?,?,?,?,?,?,?)";
			conn = this.getConn();
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);
			ps.setString(1, resource_info.getId());
			ps.setString(2, resource_info.getName());
			ps.setString(3, resource_info.getManagerid());
			ps.setString(4, resource_info.getManagername());
			ps.setString(5, resource_info.getNote());
			ps.setString(6, resource_info.getType());
			ps.setString(7, resource_info.getStatus());
			ps.setString(8, resource_info.getMxdxid());
			ps.setString(9, resource_info.getMxdxmc());
			ps.setString(10, resource_info.getMxdx());
			ps.setString(11, resource_info.getZindex());
			ps.executeUpdate();
			closeStmt();
		} catch (SQLException e) {
			System.out.println(sql);
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				System.out.println(sql);
				e1.printStackTrace();
			}
			res = false;
		} finally {
			try {
				conn.commit();
			} catch (SQLException e) {
				System.out.println(sql);
				e.printStackTrace();
			}
			closeConn();
		}
		return res;
	}
}